package org.example.zuoye.util;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @Author xinchen
 * @Date: 2024/08/14/ 10:57
 * @Description 手动响应json数据
 */
public class WebAppUtils {

    /**
     * 响应一个json数据
     */
    public static void writeJson(HttpServletResponse resp,String json) {
        resp.setContentType("application/json;charset=utf-8");
        try {
            PrintWriter out = resp.getWriter();
            out.write(json);
            out.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 重载方法
     * @param resp
     * @param map
     */
    public static void writeJson(HttpServletResponse resp, Map<String,Object> map) {
        writeJson(resp, JSON.toJSONString(map));
    }
}
